import wx
import wx.lib.newevent
import math
import logging


logging.basicConfig(
    format='%(asctime)s : %(levelname)s: %(message)s',
    level=logging.DEBUG)


ValueDragEvent, EVT_VALUE_DRAG = wx.lib.newevent.NewEvent()


class TextCtrl(wx.TextCtrl):
    def __init__(self, *args, **kw):
        super(TextCtrl, self).__init__(*args, **kw)

        self.period = 50

        self.Bind(wx.EVT_LEFT_DOWN, self.onMouseEvent)
        self.Bind(wx.EVT_MOTION, self.onMouseEvent)

    def getMouseStatus(self):
        mousePos = wx.GetMousePosition()
        mouseState = wx.GetMouseState()
        if not mouseState.LeftIsDown():
            return

        dx = (mousePos - self.prevMousePos).x
        self.prevMousePos = mousePos
        wx.PostEvent(self, ValueDragEvent(value=dx))
        wx.CallLater(self.period, self.getMouseStatus)

    def onMouseEvent(self, event):
        if event.GetEventType() == wx.EVT_LEFT_DOWN.typeId:
            self.prevMousePos = wx.GetMousePosition()
            self.getMouseStatus()
            event.Skip()


class NumTextCtrl(TextCtrl):
    def __init__(self, *args, **kw):
        super(NumTextCtrl, self).__init__(*args, **kw)

        self.__precision = 2
        self.step = 0.01

        self.Bind(EVT_VALUE_DRAG, self.onValueDrag)

    def onValueDrag(self, e):
        value = float(self.GetValue())
        value = round(e.value * self.step + value, self.precision)
        self.SetValue(str(value))

    @property
    def precision(self):
        return self.__precision

    @precision.setter
    def precision(self, precision):
        self.__precision = precision
        self.step = math.pow(0.1, self.__precision)


class NumText(wx.TopLevelWindow):
    def __init__(self, *args, **kw):
        super(NumText, self).__init__(*args, **kw)

        self.Bind(wx.EVT_PAINT, self.onPaint)
        logging.debug(self.CanSetTransparent())

    def onPaint(self, e):
        dc = wx.PaintDC(self)
        brush = wx.Brush(
            wx.Colour(255, 255, 255, 255))

        dc.SetPen(wx.Pen(wx.Colour(0, 0, 0)))
        dc.SetBrush(brush)
        dc.DrawRectangle((0, 0), self.GetSize())


class TestFrame(wx.Frame):
    def __init__(self, *args, **kw):
        super(TestFrame, self).__init__(*args, **kw)
        panel = wx.Panel(self)

        # wx.TextCtrl(panel, value='hello world')
        tc = NumTextCtrl(
            panel, value='1',
            style=wx.TRANSPARENT_WINDOW)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(tc, wx.SizerFlags().Expand())

        panel.SetSizer(sizer)

    def onMotion(self, e):
        logging.debug('parent motion')


if __name__ == '__main__':
    app = wx.App()
    frame = TestFrame(None, title='Hello World')
    frame.Show()
    app.MainLoop()
